package com.yantong.controller.product;

import com.yantong.pojo.ProductGrade;
import com.yantong.service.ProductGradeService;
import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 商品类别控制器
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 17:11
 */
@Controller
@RequestMapping("/centre/productGrade")
public class ProductGradeController extends BaseController {

	@Autowired
	private ProductGradeService productGradeService;

	private static final String prefix = "centre/productGrade";



	/**
	 * 页面
	 *
	 * @return
	 */
	@GetMapping("/productGrade")
	@RequiresPermissions("centre:productGrade:productGrade")
	public String industryBrand(Model model) {

		return prefix + "/productGrade";
	}

	/**
	 * 列表数据
	 *
	 * @param productGrade
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("centre:productGrade:productGrade")
	@ResponseBody
	public PageUtils<ProductGrade> listproductGrade(ProductGrade productGrade, int offset, int limit) {
		return productGradeService.listProductGrade(productGrade, offset, limit);
	}

	/**
	 * 编辑页面
	 * @param productGradeId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{productGradeId}")
	@RequiresPermissions("centre:productGrade:edit")
	public String edit(@PathVariable Long productGradeId, Model model) {
		model.addAttribute("productGrade", productGradeService.getProductGradeById(productGradeId));
		return prefix + "/edit";
	}

	/**
	 * 编辑操作
	 * @param productGrade
	 * @return
	 */
	@Log(module = "商品类别管理", operation = "编辑")
	@ResponseBody
	@RequiresPermissions("centre:productGrade:edit")
	@PostMapping("/update")
	public ResultVo update(ProductGrade productGrade) {
		if (productGrade == null || productGrade.getId() == null) {
			return ResultVo.error();
		}
		if (productGradeService.updateProductGrade(productGrade) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("centre:productGrade:add")
	@GetMapping("/add")
	public String add(Model model) {
		return prefix +"/add";
	}


	@Log(module = "商品类别管理", operation = "添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("centre:productGrade:add")
	public ResultVo saveProductGrade(ProductGrade productGrade) {
		if (productGrade == null) {
			return ResultVo.error();
		}
		if (productGradeService.saveProductGrade(productGrade) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 移除操作
	 * @param id
	 * @return
	 */
	@Log(module = "商品类别管理", operation = "移除商品类别")
	@ResponseBody
	@RequiresPermissions("centre:productGrade:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return ResultVo.error();
		}
		if (productGradeService.removeProductGrade(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

}
